package com.sx.pipeline.ui.group

import android.app.Application
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.repos.AppRepos
import com.sx.pipeline.repos.http.res.ContactUserVo
import com.sx.pipeline.repos.http.res.GroupDetailVo
import com.sx.pipeline.utils.CacheDataManager

class GroupManagerVm(app: Application): CYBaseViewModel(app) {

    var hasAdmin= false
    var groupLiveListData=SingleLiveEvent<List<ContactUserVo>>()

    var checkContactUserVo = mutableListOf<ContactUserVo>()
    fun groupInfoById(id: String){
        addSubscribe(mModel.groupListInfo(id)
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<GroupDetailVo, BaseViewModel<*>>(this, true) {
                override fun onResult(result: GroupDetailVo) {
                    result.hasAdmin?.let { hasAdmin=it }
                    result.contactList?.let {
                        groupLiveListData.postValue(it)
                    }

                }

                override fun onFailed(code: Int, msg: String?) {
                    super.onFailed(code, msg)
                    ToastUtils.showShort(msg)
                }

            })
        )
    }

    fun exitGroup(id: Int){
        addSubscribe(mModel.exitGroup(id)
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>(this, true) {
                override fun onResult(result: Any) {
                    ToastUtils.showShort("你已退出群聊")
                }

                override fun onFailed(code: Int, msg: String?) {
                    super.onFailed(code, msg)
                    ToastUtils.showShort(msg)
                }

            }))
    }

    fun addGroup(groupId: String){
        var uids=checkContactUserVo.map { it.userUnitId }.joinToString(",")
        addSubscribe(mModel.addGroup(groupId,uids)
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>(this, true) {
                override fun onResult(result: Any) {
                    ToastUtils.showShort("添加成功")
                    finish()
                }

                override fun onFailed(code: Int, msg: String?) {
                    super.onFailed(code, msg)
                    ToastUtils.showShort(msg)
                }

            }))
    }


    fun groupListById(id: String){
        addSubscribe(mModel.groupListInfo(id)
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<GroupDetailVo, BaseViewModel<*>>(this, true) {
                override fun onResult(result: GroupDetailVo) {
                    result.hasAdmin?.let { hasAdmin=it }
                    result.contactList?.let {
                        contactListEvent.postValue(it)
                    }

                }

                override fun onFailed(code: Int, msg: String?) {
                    super.onFailed(code, msg)
                    ToastUtils.showShort(msg)
                }

            })
        )
    }

    fun removeGroup(groupId: String){
        var uids=checkContactUserVo.map { it.userUnitId }.joinToString(",")
        addSubscribe(mModel.quitGroup(groupId,uids)
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>(this, true) {
                override fun onResult(result: Any) {
                    ToastUtils.showShort("删除成功")
                    finish()
                }

                override fun onFailed(code: Int, msg: String?) {
                    super.onFailed(code, msg)
                    ToastUtils.showShort(msg)
                }

            }))
    }

    fun cleanGroup(groupId: String){
        addSubscribe(mModel.cleanGroup(groupId.toInt())
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>(this, true) {
                override fun onResult(result: Any) {
                    ToastUtils.showShort("成功解散该群聊")
                    finish()
                }

                override fun onFailed(code: Int, msg: String?) {
                    super.onFailed(code, msg)
                    ToastUtils.showShort(msg)
                }

            }))
    }
}